home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / mc302emb.zip / DICE.C < prev    next >
C/C++ Source or Header  |  1994-03-18  |  2KB  |  61 lines

  1. /*
  2.  * This program implements a "soft" die using the C-FLEA
  3.  *
  4.  * A LED display is driven by ports P0-P3 in the following configuration
  5.  *
  6.  * P1: 1100110011
  7.  * P2: 0000110000
  8.  * P3: 1100110011
  9.  * 
  10.  * Multiple ports/bits are used to make the die more visible from the
  11.  * EMCF simulator display. If physical LEDS were used, only a single
  12.  * port would be required, with a simpler translate table.
  13.  *
  14.  * Rolling of the die is triggered by the reception of any character
  15.  * from the keyboard. In a physical hardware implementation, a momentary
  16.  * contact switch could be connected to an unused port input.
  17.  *
  18.  * The display is assumed to be ACTIVE HIGH (writeing 1 turns on the LED)
  19.  * You can change this to ACTIVE LOW by inverting the bits in the translate
  20.  */
  21. #include cflea.h
  22.  
  23. #define    DELAY    20000        /* Adjust this value to vary ROLL delay */
  24.  
  25. char *translate_table[6][3] = {
  26.     0x00, 0x18, 0x00,        /* 1 */
  27.     0xC0, 0x00, 0x03,        /* 2 */
  28.     0x03, 0x18, 0xC0,        /* 3 */
  29.     0xC3, 0x00, 0xC3,        /* 4 */
  30.     0xC3, 0x18, 0xC3,        /* 5 */
  31.     0xDB, 0x00, 0xDB };        /* 6 */
  32.  
  33. main()
  34. {
  35.     unsigned char old, new;
  36.     unsigned i, j;
  37.  
  38.     old = 5;                /* First wraps to 1 */
  39.     outp1(0);
  40.     outp2(0);
  41.     outp3(0);
  42.  
  43.     putstr("\nPress any key to roll die:\n");
  44.  
  45.     while(!chkch())            /* Wait for first keypress */
  46.         rand();                /* Cycle random number generator */
  47.  
  48.     /* Roll dice & cycle to new value */
  49.     for(;;) {
  50.         putstr("Rolling ");
  51.         new = rand(6);
  52.         for(i=0; (i < 6) || (old != new); ++i) {
  53.             old = (old + 1) % 6;
  54.             outp1(translate_table[old][0]);
  55.             outp2(translate_table[old][1]);
  56.             outp3(translate_table[old][2]);
  57.             for(j=0; j < DELAY; ++j); }
  58.         putch('\n');
  59.         getch(); }    /* Wait for next button pressed */
  60. }
  61.